//  SubpixelResample.m
//  SubpixelResample
//
//  Created by David Rudie on 2009 May 9.
//  Copyright 2009 David Rudie. All rights reserved.
//
//  This program is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
//


#import "SubpixelResample.h"

@implementation SubpixelResample

@synthesize cropAspectRatio = _cropAspectRatio;
@synthesize debugEnabled = _debugEnabled;
@synthesize embedFilename = _embedFilename;
@synthesize embedFilesize = _embedFilesize;
@synthesize fontFile = _fontFile;
@synthesize maxSize = _maxSize;
@synthesize subpixelResample = _subpixelResample;
@synthesize thumbnailPrefix = _thumbnailPrefix;
@synthesize imageSize = _imageSize;
@synthesize croppedSize = _croppedSize;

- (id)init {
	if ((self = [super init])) {
		NSString *errorDescription = nil;
		NSPropertyListFormat format;
		NSString *plistPath = [NSBundle pathForResource:@"Configuration" ofType:@"plist" inDirectory:@"./"];
		NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:plistPath];
		NSDictionary *configurationData = (NSDictionary *)[NSPropertyListSerialization
														   propertyListFromData:plistXML
														   mutabilityOption:NSPropertyListMutableContainersAndLeaves
														   format:&format errorDescription:&errorDescription];
		
		if (!configurationData) {
			NSLog(errorDescription);
			NSLog(@"Using defaults...");
			[errorDescription release];
		}
		
		// crop aspect ratio -- default 0.0
		if (!(self.cropAspectRatio = [configurationData objectForKey:@"cropAspectRatio"]))
			self.cropAspectRatio = [NSNumber numberWithDouble:0.0];
		
		// debug output -- default NO
		if (!(self.debugEnabled = [configurationData objectForKey:@"debugEnabled"]))
			self.debugEnabled = [NSNumber numberWithBool:NO];
		
		// embed filename on image -- default NO
		if (!(self.embedFilename = [configurationData objectForKey:@"embedFilename"]))
			self.embedFilename = [NSNumber numberWithBool:NO];
		
		// embed resolution and filesize on image -- default NO
		if (!(self.embedFilesize = [configurationData objectForKey:@"embedFilesize"]))
			self.embedFilesize = [NSNumber numberWithBool:NO];
		
		// font file to use -- default blank
		if (!(self.fontFile = [configurationData objectForKey:@"fontFile"]))
			self.fontFile = [NSString stringWithString:@""];
		
		// maximum resolution -- default 160
		if (!(self.maxSize = [configurationData objectForKey:@"maxSize"]))
			self.maxSize = [NSNumber numberWithInt:160];
		
		// use subpixel resampling -- default YES
		if (!(self.subpixelResample = [configurationData objectForKey:@"subpixelResample"]))
			self.subpixelResample = [NSNumber numberWithBool:YES];
		
		// prefix for thumbnail filename -- default t_
		if (!(self.thumbnailPrefix = [configurationData objectForKey:@"thumbnailPrefix"]))
			self.thumbnailPrefix = [NSString stringWithString:@"t_"];
	}

	return self;
}

- (void)dealloc {
	[super dealloc];
}

- (void)cropResolution {
	if ([self.cropAspectRatio doubleValue] <= 0.0) {
		CGFloat divideBy;
		if (self.imageSize.width > self.imageSize.height)
			divideBy = self.imageSize.width / [self.maxSize intValue];
		else
			divideBy = self.imageSize.height / [self.maxSize intValue];
		
		self.croppedSize = NSMakeSize(self.imageSize.width / divideBy, self.imageSize.height / divideBy);
	} else if ([self.cropAspectRatio doubleValue] > 0.0 && [self.cropAspectRatio doubleValue] < 1.0) {
		self.croppedSize = NSMakeSize([self.maxSize intValue] * [self.cropAspectRatio doubleValue], [self.maxSize intValue]);
	} else if ([self.cropAspectRatio doubleValue] == 1.0) {
		self.croppedSize = NSMakeSize([self.maxSize intValue], [self.maxSize intValue]);
	} else if ([self.cropAspectRatio doubleValue] > 1.0) {
		self.croppedSize = NSMakeSize([self.maxSize intValue], [self.maxSize intValue] / [self.cropAspectRatio doubleValue]);
	}
}

- (void)printConfiguration {
	BOOL debug = [self.debugEnabled boolValue];
	if (debug == YES) {
		NSLog(@"Configuration...");
		NSLog(@"cropAspectRatio: %lg", [self.cropAspectRatio doubleValue]);
		NSLog(@"debugEnabled: %i", [self.debugEnabled boolValue]);
		NSLog(@"embedFilename: %i", [self.embedFilename boolValue]);
		NSLog(@"embedFilesize: %i", [self.embedFilesize boolValue]);
		NSLog(@"fontFile: %s", [self.fontFile cStringUsingEncoding:NSUTF8StringEncoding]);
		NSLog(@"maxSize: %i", [self.maxSize intValue]);
		NSLog(@"subpixelResample: %i", [self.subpixelResample boolValue]);
		NSLog(@"thumbnailPrefix: %s", [self.thumbnailPrefix cStringUsingEncoding:NSUTF8StringEncoding]);
	}
}

- (NSString *)stringFromFileSize:(size_t)fileSize {
	CGFloat floatSize = fileSize;
	
	if (fileSize < 1023)
		return ([NSString stringWithFormat:@"%iB", fileSize]);
	
	floatSize /= 1024;
	if (floatSize < 1023)
		return ([NSString stringWithFormat:@"%.2fKB", floatSize]);
	
	floatSize /= 1024;
	if (floatSize < 1023)
		return ([NSString stringWithFormat:@"%.2fMB", floatSize]);
	
	floatSize /= 1024;
	return ([NSString stringWithFormat:@"%.2fGB", floatSize]);
}

@end
